package com.iwords.android;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.client.methods.HttpPost;
import org.json.JSONObject;

import android.app.Application;
import android.widget.Toast;

public class IWordsApp extends Application {
	private User user = new User();
	private boolean needPay = false;
	//当前课本的全部单词
	private List<WordStatus> currentLibWords = new ArrayList<WordStatus>();
	/**用户当前词库Id*/
	private Long currentWordLibId = 0L;
	/**当前词库状态*/
	private WordsLibStatus currentLibStatus;

	//记忆等级的顺序
	private List<KnownLevel> knownLevelOrder;
	
	public IWordsApp(){
		knownLevelOrder = new ArrayList<KnownLevel>();
		knownLevelOrder.add(KnownLevel.Known);
		knownLevelOrder.add(KnownLevel.Level5);
		knownLevelOrder.add(KnownLevel.Level4);
		knownLevelOrder.add(KnownLevel.Level3);
		knownLevelOrder.add(KnownLevel.Level2);
		knownLevelOrder.add(KnownLevel.Level1);
		knownLevelOrder.add(KnownLevel.Unknown);
	}

	public boolean isNeedPay() {
		return needPay;
	}

	public void setNeedPay(boolean needPay) {
		this.needPay = needPay;
	}

	public WordsLibStatus getCurrentLibStatus() {
		return currentLibStatus;
	}

	public void setCurrentLibStatus(WordsLibStatus currentLibStatus) {
		this.currentLibStatus = currentLibStatus;
	}

	public Long getCurrentWordLibId() {
		return currentWordLibId;
	}

	public void setCurrentWordLibId(Long currentWordLibId) {
		this.currentWordLibId = currentWordLibId;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}
	
	public KnownLevel getKnownLevel(int index){
		return this.knownLevelOrder.get(index);
	}
	public int getKnownLevelsSize(){
		return this.knownLevelOrder.size();
	}

	public List<WordStatus> getCurrentLibWords() {
		return currentLibWords;
	}

	public void setCurrentLibWords(List<WordStatus> currentLibWords) {
		this.currentLibWords = currentLibWords;
	}
	
	private JSONObject userSettings = new JSONObject();
	public void loadUserSettings(){
		String href = this.getResources().getString(R.string.user_find_config) + ((IWordsApp) getApplicationContext()).getUser().getUserid();
		HttpPost request = new HttpPost(href);
		//System.out.println("UserSettings.getUserSettings()! href="+href);
		try {
			JSONObject jSONObject = ServerAccessor.access(this, request);
			boolean success = jSONObject.getBoolean("isSuccess");
			if(success) {
				this.userSettings = jSONObject.getJSONObject("defaultValue");
				//System.out.println("@@@@@@@IWordsApp.loadUserSetting() userSettiongs = "+userSettings);
			} else {
				this.displayMessage(CodeUtils.decode(jSONObject.getString("message")));
			}
		}catch(Exception e){
			//e.printStackTrace();
			this.displayMessage(e.getMessage());
		}
	}
	/**查询用户的注册信息。*/
	public JSONObject getUserSettings(){
		if(userSettings != null) loadUserSettings();
		return userSettings;
	}
	private void displayMessage(String message){
		Toast t1 = Toast.makeText(this, message, Toast.LENGTH_LONG);
		t1.show();
	}	
	
	/**切换词库，切换用户时，清理主流内存中的数据。*/
	public void clear(){
		this.currentLibStatus = null;
	}
	
}
